/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Form.Module.Fields.CountryLanguage = Ext.extend(Ext.Panel, {

    /**
     * Constructor
     *
     * @param   {Object}    baseConfig
     */
    constructor: function(baseConfig) {
        this.countryCombo = new Ext.form.ComboBox({
                            store:              new Ext.data.JsonStore({
                                                    id:             'id',
                                                    root:           'result',
                                                    fields:         [{ name: 'id', mapping: 'realId' }, { name: 'name', mapping: 'name' }],
                                                    autoLoad:       true,
                                                    totalProperty:  'count',
                                                    remoteSort:     true,
                                                    url:            Serum.Selector.get().requestUrl(),
                                                    baseParams:     {
                                                                        method: Serum.Selector.get().method({ module: 'Languages', controller: 'Index', action: 'index' }),
                                                                        requestId: Serum.Helper.Request.id()
                                                                    },
                                                    listeners: {
                                                        'load': function() {
                                                            if (typeof baseConfig['country_id'] === 'undefined' || baseConfig['country_id'] === '') {
                                                                return;
                                                            }

                                                            this.countryCombo.setValue(baseConfig['country_id']);
                                                            this.countryCombo.fireEvent('select');
                                                        },

                                                        scope: this
                                                    }
                                                }),

                             fieldLabel:        __('languages~form_label_DbCountry'),
                             displayField:      'name',
                             valueField:        'id',
                             mode:              'remote',
                             forceSelection:    true,
                             triggerAction:     'all',
                             scope:             this,
                             hiddenName:        'rel-DbCountry',
                             //value:             baseConfig['value'],

                             listeners: {
                                 select: function() {
                                     if (this.countryCombo.getValue() !== '') {
                                        this.languagesCombo.enable();

                                         this.languagesCombo.store.baseParams = {
                                                                            method:     Serum.Selector.get().method({ module: 'Languages', controller: 'Index', action: 'index' }),
                                                                            requestId:  Serum.Helper.Request.id(),
                                                                            parentRoot: this.countryCombo.getValue()
                                                                        };

                                         this.languagesCombo.store.on('load', function() {
                                                if (typeof baseConfig['language_id'] === 'undefined' || baseConfig['language_id'] === '') {
                                                    return;
                                                }

                                                this.languagesCombo.setValue(baseConfig['language_id']);
                                         }, this);

                                         this.languagesCombo.clearValue();
                                         this.languagesCombo.store.load();
                                     }
                                 },

                                 scope: this
                             }
        });

        this.languagesCombo = new Ext.form.ComboBox({
                            store:              new Ext.data.JsonStore({
                                                    id:             'id',
                                                    root:           'result',
                                                    fields:         [{ name: 'id', mapping: 'realId' }, { name: 'name', mapping: 'name' }],
                                                    autoLoad:       false,
                                                    totalProperty:  'count',
                                                    remoteSort:     true,
                                                    url:            Serum.Selector.get().requestUrl(),
                                                    baseParams:     {
                                                                        method:     Serum.Selector.get().method({ module: 'Languages', controller: 'Index', action: 'index' }),
                                                                        requestId:  Serum.Helper.Request.id()
                                                                    }
                                                }),

                             fieldLabel:        __('languages~form_label_DbLanguage'),
                             displayField:      'name',
                             valueField:        'id',
                             mode:              'remote',
                             disabled:          true,
                             forceSelection:    true,
                             triggerAction:     'all',
                             selectOnFocus:     true,
                             hiddenName:        'rel-DbLanguage'
        });

        Serum.Form.Module.Fields.CountryLanguage.superclass.constructor.call(this, Ext.apply({
            border:     false,
            layout:     'form',
            items:      [
                            this.countryCombo,
                            this.languagesCombo
                        ]

        }, baseConfig));
    }

});


Ext.reg('countrylanguagefield', Serum.Form.Module.Fields.CountryLanguage);